Course: 1. Getting started | 2. Modeling with Java | 3. Automated testing | 4. Inheritance | 5. Basic business logic | 6. Advanced validation | 7. Refining the standard behavior | 8. Behavior & business logic | 9. References & collections | A. Architecture & philosophy | B. Java Persistence API | C. Annotations

Lesson 6: Advanced validation

This lesson is available in PDF:


Note for OpenXava 5.3 and better

Since OpenXava 5.3 the new Hibernate Validator included in OpenXava uses the standard Bean Validation API instead of its own API, therefore the code in the section 6.2 of this lesson does not work as is with OpenXava 5.3. Use the next code instead:

ISBNValidator.java
package org.openxava.facturacion.validators;
 
import javax.validation.*;
 
import org.apache.commons.logging.*;
import org.openxava.facturacion.annotations.*;
import org.openxava.util.*;
 
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
 
public class ISBNValidator implements ConstraintValidator<ISBN, Object> {
 
    private static Log log = LogFactory.getLog(ISBNValidator.class);
    private static org.apache.commons.validator.routines.ISBNValidator
        validator = new org.apache.commons.validator.routines.ISBNValidator();
    private boolean search;
 
    public void initialize(ISBN isbn) {
        this.search = isbn.search();
    }
 
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (Is.empty(value)) return true;
        if(!validator.isValid(value.toString())) return false;
        return search?isbnExists(value):true;
    }
 
    private boolean isbnExists(Object isbn) {
        try {
            WebClient client = new WebClient();
            HtmlPage page = (HtmlPage) client.getPage("http://www.bookfinder4u.com/" +
            "IsbnSearch.aspx?isbn=" + isbn + "&mode=direct");
 
            return page.asText().indexOf("ISBN: " + isbn) >= 0;
        } catch (Exception ex) {
            log.warn("Impossible to connect to bookfinder4u" +
            "to validate the ISBN. Validation fails", ex);
            return false;
        }
    }
}

ISBN.java:
package org.openxava.facturacion.annotations;
 
import java.lang.annotation.*;
 
import javax.validation.*;
 
import org.openxava.facturacion.validators.*;
 
@Constraint(validatedBy = ISBNValidator.class)
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ISBN {
 
    Class<?>[] groups() default{};
    Class<? extends Payload>[] payload() default{};
 
    boolean search() default true;
    String message() default "ISBN does not exist";
}

You need to copy the following libraries from OpenXava/lib to Invoicing/web/WEB-INF/lib: cssparser.jar, htmlunit.jar, htmlunit-core-js.jar, httpclient.jar, httpcore.jar, httpmime.jar, nekothml.jar, sac.jar, xalan.jar, xercesImpl.jar. Also you need to download the following library and add it into the same folder: xml-apis-2.10.0.jar

Any problem with this lesson? Ask in the forum Everything fine? Go to Lesson 7